<#macro scriptMacro>
  <script>
    layui.use(['form', 'table'], function () {
      var $ = layui.jquery,
          form = layui.form,
          table = layui.table;

      table.render({
        elem: '#chainListTable',
        request: {
          pageName: 'current',
          limitName: 'size'
        },
        where: {
          auditStatus: $('select[name=auditStatus]').val()
        },
        url: '${request.contextPath}/friendChain/pageByQuery',
        method: 'POST',
        cols: [[
          {field: 'friendName', width: 150, title: '友链名称'},
          {field: 'friendAvatar', width: 150, title: '友链头像'},
          {field: 'friendSite', width: 200, title: '友链网址'},
          {field: 'friendDescription', width: 150, title: '友链简介'},
          {field: 'auditStatusDesc', width: 100, title: '审核状态'},
          {field: 'auditRemark', width: 350, title: '审核备注'},
          {
            title: '创建时间',
            minWidth: 150,
            sort: true,
            templet: '<div>{{ layui.util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss") }}</div>'
          },
          {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
        ]],
        limits: [10, 15, 20, 25, 50, 100],
        limit: 10,
        page: true,
        skin: 'line',
        parseData: function (res) {
          var page = res.data;
          if (page) {
            currentPage = page.current;
            return {
              "code": res.code,
              "msg": res.msg,
              "count": page.total,
              "data": page.records
            }
          } else {
            return {
              "code": res.code,
              "msg": res.msg
            }
          }
        },
      });

      table.on('tool(chainListTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'delete') {
          layer.confirm('真的删除行么', function (index) {
            $.post("${request.contextPath}/friendChain/delete", {
              id: data.id
            }, function (result) {
              layMsg(result.msg);
              if (result.success) {
                layer.close(index);
                reloadTable();
              }
            })
          });
        } else if (obj.event === 'pass') {
          doModifyStatus(data.id,'pass');
        } else if (obj.event === 'reject') {
          doModifyStatus(data.id,'reject');
        } else if (obj.event === 'reset') {
          doModifyStatus(data.id,'reset');
        }
      });

      $("#search-btn").on('click', function () {
        reloadTable(1);
      })
    });

    var currentPage = 1;
    function doModifyStatus(id, status) {
      var $ = layui.jquery;
      $.post("${request.contextPath}/friendChain/modifyStatus", {
        id: id,
        auditStatus: status
      }, function (result) {
        layMsg(result.msg);
        if (result.success) {
          reloadTable();
        }
      })
    }

    function reloadTable(page) {
      layui.use('table', function () {
        var $ = layui.jquery, table = layui.table;
        //执行搜索重载
        table.reload('chainListTable', {
          page: {
            curr: page ? page : currentPage
          },
          where: {
            auditStatus: $('select[name=auditStatus]').val()
          }
        }, true);
      })
    }

  </script>
</#macro>
<@zlt.page title="友链列表" scriptMacro=scriptMacro>
  <body>
  <div class="layuimini-container">
    <div class="layuimini-main">
      <fieldset class="table-search-fieldset">
        <legend>搜索信息</legend>
        <div class="layui-form" style="margin: 10px 10px 10px 10px">
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">审核状态</label>
              <div class="layui-input-inline">
                <select name="auditStatus" lay-filter="">
                  <option value="">请选择状态</option>
                    <#list chainStatusList as s>
                      <option value="${s.value}">${s.desc}</option>
                    </#list>
                </select>
              </div>
            </div>
            <div class="layui-inline">
              <button type="button" id="search-btn" class="layui-btn layui-btn-primary loading-btn"
                      data-reset-time="1000"><i class="layui-icon"></i> 搜 索
              </button>
            </div>
          </div>
        </div>
      </fieldset>

      <table class="layui-hide" id="chainListTable" lay-filter="chainListTableFilter"></table>

      <script type="text/html" id="currentTableBar">
        {{# if(d.auditStatus=='${FriendChainAuditStatusEnum.APPLY.getValue()}'){ }}
        <a class="layui-btn layui-btn-xs" lay-event="pass">通过</a>
        <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="reject">不通过</a>
        {{# } }}
        {{# if(d.auditStatus=='${FriendChainAuditStatusEnum.REJECT.getValue()}'){ }}
        <a class="layui-btn layui-btn-success layui-btn-xs" lay-event="reset">重置</a>
        {{# } }}
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
      </script>
    </div>
  </div>
  </body>
</@zlt.page>